{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Overview"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After you train your model, you can deploy it to get predictions in one of two ways:\n",
    "\n",
    "* Deploy the model as an HTTPS endpoint using Amazon SageMaker hosting services.\n",
    "* Perform batch predictions for an entire dataset using Amazon SageMaker batch transform."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Deploy Model as an HTTPS Endpoint"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Amazon SageMaker provides model hosting services for model deployment, as shown in the following diagram. \n",
    "Amazon SageMaker provides an HTTPS endpoint where your machine learning model is available to provide inferences."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"img/realtime_inference.png\" width=\"70%\" align=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "predictor = [estimator|model].deploy(initial_instance_count=1,\n",
    "                         content_type='text/csv',\n",
    "                         instance_type='ml.t2.medium')\n",
    "                         \n",
    "predictor.predict(data)\n",
    "```\n",
    "                            \n",
    "The `deploy()` method creates the deployable model, configures the Amazon SageMaker hosting services endpoint, and launches the endpoint to host the model. It also returns a `sagemaker.predictor.RealTimePredictor` object, which you can use to get inferences from the model."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Levels Of Customization \n",
    "\n",
    "* (Option 1) Pre-built code and pre-built algorithm container\n",
    "\n",
    "* (Option 2) Bring your own code and pre-built framework container\n",
    "\n",
    "* (Option 3) Bring your own code and custom container"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Perform Batch Predictions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use batch transform when:\n",
    "* You want to get inferences for an entire dataset\n",
    "* You don't need a persistent endpoint that applications (for example, web or mobile apps) can call to get inferences\n",
    "* You don't need the subsecond latency that Amazon SageMaker hosted endpoints provide"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"img/batch_inference.png\" width=\"80%\" align=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "# Only instance_type and instance_count are required.\n",
    "transformer = sm_model.transformer(instance_type='ml.c5.xlarge',\n",
    "                                   instance_count=1,\n",
    "                                   strategy='MultiRecord',\n",
    "                                   max_payload=6,\n",
    "                                   max_concurrent_transforms=8,\n",
    "                                   accept='text/csv',\n",
    "                                   assemble_with='Line',\n",
    "                                   output_path='s3://my-output-bucket/path/to/my/output/data/')\n",
    "\n",
    "# Only data is required.\n",
    "transformer.transform(data='s3://my-input-bucket/path/to/my/csv/data',\n",
    "                      content_type='text/csv',\n",
    "                      split_type='Line')\n",
    "\n",
    "# Waits for the Transform Job to finish.\n",
    "transformer.wait()\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%html\n",
    "\n",
    "<p><b>Shutting down your kernel for this notebook to release resources.</b></p>\n",
    "<button class=\"sm-command-button\" data-commandlinker-command=\"kernelmenu:shutdown\" style=\"display:none;\">Shutdown Kernel</button>\n",
    "        \n",
    "<script>\n",
    "try {\n",
    "    els = document.getElementsByClassName(\"sm-command-button\");\n",
    "    els[0].click();\n",
    "}\n",
    "catch(err) {\n",
    "    // NoOp\n",
    "}    \n",
    "</script>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%javascript\n",
    "\n",
    "try {\n",
    "    Jupyter.notebook.save_checkpoint();\n",
    "    Jupyter.notebook.session.delete();\n",
    "}\n",
    "catch(err) {\n",
    "    // NoOp\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "instance_type": "ml.t3.medium",
  "kernelspec": {
   "display_name": "Python 3 (Data Science)",
   "language": "python",
   "name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-east-1:081325390199:image/datascience-1.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
